import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:untitleqianru/word_detaile_dialog.dart';

import 'main.dart';

class FavoritesDialog {
  final GlobalKey webViewKey = GlobalKey();
  void build1(
    BuildContext context,
  ) {
    List<WordItem> wordList = [];
    daoCase.getWordFavorite().then((data) {
      for (int i = 0; i < data.length; i++) {
        print("getWordByWord");
        print(data);

        wordList.add(WordItem(
            id: data[i]['_id'] as int,
            word: data[i]['_word'] as String,
            meaning: data[i]['_meaning'] as String,
            voice: data[i]['_voice'] as String));
      }
    }).catchError((err) {
      print('daoCase.getCaseAll()___error');
      print(err);
    });




    showDialog(
        context: context,
        builder: (_) => Padding(
              padding:
                  EdgeInsets.only(top: 30, left: 15, right: 15, bottom: 75),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: Scaffold(
                  body: ListView.separated(
                      itemCount: wordList.length,
                      itemBuilder: (context, index) {
                        return WordCard(
                          data: wordList[index],
                        );
                      },
                      separatorBuilder: (context, index) {
                        return const Divider(
                          height: .6,
                          indent: 0,
                          color: Color(0xFF555555),
                        );
                      }),
                ),
              ),
            ));
  }
}

class WordCard extends StatelessWidget {
  final WordItem data;
  final GlobalKey webViewKey = GlobalKey();

  WordCard({
    required this.data,
  }) : super();

  @override
  Widget build(BuildContext context) {
    // String shotCon = data.meaning;
    String shotCon = "";
    print(data.word);
    final width =MediaQuery.of(context).size.width;
    print("width");
    print(width);
    List<String> means = data.meaning.split("\n");
    for(int i=0;i<means.length;i++){
      List<String> mean = means[i].split("；");
      String tmp = mean[0];
      for(int j=1;j<mean.length;j++){

        if (tmp.toString().length + mean[j].toString().length  > 23) {
          tmp = tmp + "……";
          break;
        }
        else{
          tmp = tmp + "；" + mean[j];
        }
      }

      shotCon = shotCon + tmp.toString();
      if(i+1<means.length){
        shotCon = shotCon + "\n";
      }
    }

    return Center(
      child: Material(
        //INK可以实现装饰容器，实现矩形 设置背景色
        child: InkWell(
          //点击事件回调
          onTap: () {
            print("点击到第" + data.id.toString());
            CaseDialog().build1(context, data.word);
          },
          // onLongPress: () {
          //   print("长按" + this.data.id.toString());
          // },
          child: Container(
            alignment: Alignment.centerLeft,
            // color: Colors.white,
            padding: EdgeInsets.all(12),
            child: Column(
              //
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                  Text(
                    data.word,
                    style: const TextStyle(
                      fontSize: 21,
                      fontWeight: FontWeight.w500,
                      color: Color(0xFF000000),
                    ),
                  ),
                      // const Padding(padding: EdgeInsets.all(1)),
                  Text(
                    data.voice,
                    style: const TextStyle(
                      fontSize: 13,
                      fontWeight: FontWeight.w400,
                      color: Color(0xFF555555),
                    ),
                  ),
                ])),
                const Padding(padding: EdgeInsets.only(top: 5)),
                Text(
                  shotCon,
                  style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.w400,
                    color: Color(0xFF333333),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class WordItem {

  final int id;

  final String word;

  final String meaning;

  final String voice;

  const WordItem(
      {required this.id,
      required this.word,
      required this.meaning,
      required this.voice});
}
